#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

struct Student
{
	char name[20];
	int age;
	double score;
};

int main()
{
	int n;
	printf("请输入学生人数:");
	scanf("%d",&n);

	//文件映射虚拟内存：
	int fd = open("stu.dat",O_RDWR|O_CREAT,0666);
	ftruncate(fd,n*sizeof(struct Student));
	char* pTemp = mmap(NULL,n*sizeof(struct Student),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	if(NULL == pTemp)
		printf("映射虚拟内存失败:%m\n"),exit(-1);
	printf("映射虚拟内存:%m\n");


	struct Student* p = (struct Student*)pTemp;
	for(int i=0;i<n;i++)
	{
		printf("请输入第%d个学生的名字：",i+1);
		scanf("%s",p->name);

		printf("请输入第%d个学生的年龄：",i+1);
		scanf("%d",&(p->age));

		printf("请输入第%d个学生的成绩：",i+1);
		scanf("%lf",&(p->score));

		printf("over\n");
		p++;
	}


	munmap(pTemp,n*sizeof(struct Student));
	close(fd);

	return 0;
}